package com.medical.patient.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.medical.common.utils.PageUtils;
import com.medical.common.utils.R;
import com.medical.patient.vo.UpdatePwdVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.medical.patient.entity.PatientPatientEntity;
import com.medical.patient.service.PatientPatientService;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-08-13 18:42:37
 */
@RestController
@RequestMapping("patient/patientpatient")
public class PatientPatientController {
    @Autowired
    private PatientPatientService patientPatientService;

    @RequestMapping("patientlist")
    public List<PatientPatientEntity> patientlist(){
        return patientPatientService.list();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,@RequestBody PatientPatientEntity patientPatientEntity){
        PageUtils page = patientPatientService.queryPage(params,patientPatientEntity);

        return R.ok().put("page", page);
    }

    /**
     * 列表用户名模糊查
     */
    @RequestMapping("/listFind")
    public R listFind(@RequestBody PatientPatientEntity patientPatientEntity){
        List<PatientPatientEntity> list = patientPatientService.listByPatientName(patientPatientEntity);

        return R.ok().put("page", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{patientId}")
    public R info(@PathVariable("patientId") Integer patientId){
		PatientPatientEntity patientPatient = patientPatientService.getById(patientId);
        return R.ok().put("patientPatient", patientPatient);
    }
    /**
     * userId查询信息
     */
    @RequestMapping("/infoByUserId/{userId}")
    public R infoUserId(@PathVariable("userId") String userId){
        UpdatePwdVo updatePwdVo = patientPatientService.infoByUserId(userId);

        return R.ok().put("updatePwdVo", updatePwdVo);
    }


    /**
     * 用户id查询信息
     */
    @RequestMapping("/findByUserId/{userId}")
    public R findByUserId(@PathVariable("userId") String userId){
        PatientPatientEntity patientPatient = patientPatientService.findByUserId(userId);

        return R.ok().put("patientPatient", patientPatient);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PatientPatientEntity patientPatient){
		patientPatientService.save(patientPatient);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PatientPatientEntity patientPatient){
		patientPatientService.updateById(patientPatient);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] patientIds){
		patientPatientService.removeByIds(Arrays.asList(patientIds));

        return R.ok();
    }

    /**
     * 密码修改
     */
    @RequestMapping("/updatePwd")
    public R updatePwd(@RequestBody PatientPatientEntity patientPatient){
        patientPatientService.updatePwdById(patientPatient);

        return R.ok("用户密码修改成功");
    }
    /**
     * 密码修改2
     */
    @RequestMapping("/updatePwd2")
    public R updatePwd2(@RequestBody UpdatePwdVo updatePwdVo){
        String userOldPwd = updatePwdVo.getUserPwd();
        UpdatePwdVo old  = patientPatientService.findByOldPwd(updatePwdVo);
        if (old!=null){
            if (!old.getUserPwd().equals(userOldPwd)){
                return R.error("当前密码错误");
            }
        }
        patientPatientService.updatePwdByUserId(updatePwdVo);
        return R.ok("用户密码修改成功");

    }

    /**
     * 用户头像昵称展示
     */
    @RequestMapping("/imgNameShow/{userId}")
    public R imgNameShow(@PathVariable("userId") String userId){
        PatientPatientEntity patientPatient = patientPatientService.findImgNameByUserId(userId);

        return R.ok().put("imgNameShow", patientPatient);
    }

    /**
     * 用户头像修改
     */
    @RequestMapping("/updateImg")
    public R updateImg(@RequestBody PatientPatientEntity patientPatient){
        patientPatientService.updateImgById(patientPatient);

        return R.ok("用户头像修改成功");
    }

    /**
     * 用户昵称修改
     */
    @RequestMapping("/updateName")
    public R updateName(@RequestBody PatientPatientEntity patientPatient){
        patientPatientService.updateNameById(patientPatient);

        return R.ok("用户昵称修改成功");
    }

}
